let cp = require('cp');
require('./page.css');
cp.initPage();
let vm_member = avalon.define({
  $id: 'mc_usercenter',
  image: 'http://diy.qqjay.com/u/files/2012/0924/300bd353d29aae0f6197d92cd91adbbb.jpg',
  xuanze: 'http://diy.qqjay.com/u/files/2012/0924/300bd353d29aae0f6197d92cd91adbbb.jpg',
  toggle: true,
  password: '',
  new_password: '',
  new_password_2: '',
  org_info: cp.vm_user_info.current_org,
  user_info: {
    name: '',
    head: '',
    gender: '', // 性别
    birthday: '',
    account: {
      realname: '',
      mobile: '',
    },
  },
  validate: {
    onValidateAll: function(reasons) {
      if (reasons.length > 0) {
        cp.layerMsg({msg: reasons[0].getMessage()});
      } else {
        cp.ajaxUrl({
          controller: 'user',
          cmd: 'com.rel.porg.pb.user_info_req',
          data: {
            'req': 1,
            'info': vm_member.user_info,
          },
          success: function(res) {
            cp.initPage();
            cp.layerMsg({msg: '保存成功'});
          },
          fail: function(res) {
            cp.layerMsg({msg: '保存失败'});
          },
        });
      }
    },
  },
  validate_password: {
    onValidateAll: function(reasons) {
      if (reasons.length > 0) {
        cp.layerMsg({msg: reasons[0].getMessage()});
      } else {
        let md5 = require('js-md5');
        let Base64 = require('js-base64').Base64;
        cp.ajaxUrl({
          controller: 'account',
          cmd: 'com.rel.porg.pb.acc_passwd_repwd_req',
          data: {
            'original_passwd': Base64.encode(md5(vm_member.password).toUpperCase()),
            'new_passwd': Base64.encode(md5(vm_member.new_password_2).toUpperCase()),
          },
          success: function(res) {
            cp.layerMsg({msg: '密码修改成功'});
            setTimeout(function() {
              location.reload();
            }, 3000);
          },
          fail: res => {
            cp.layerMsg(res.desc);
          },
        });
      };
    },
  },
  initQiniu: function(article_id = 'no_id') {
    // 初始化qiniu
    let moduleExports = require('./init.qiniu.js');
    moduleExports.initQiniu();
    moduleExports.uploader.bind('FileUploaded', function(up, file, info) {
      let domain = up.getOption('domain');
      let res = JSON.parse(info.response);
      let sourceLink = domain + '/' + res.key; // 获取上传成功后的文件的Url;
      vm_member.user_info.head = sourceLink;
    });
    moduleExports.uploader.bind('BeforeUpload', function(up, file) {
      // 每个文件上传前，处理相关的事情
      up.settings.url = 'http://up-z2.qiniu.com';
    });
  },
  initDatePicker: () => {
    require.ensure(['jquery-datetimepicker'], () => {
      require('jquery-datetimepicker');
      require('jquery-datetimepicker/jquery.datetimepicker.css');
      jQuery.datetimepicker.setLocale('ch');
      jQuery('#j-date-birthday').datetimepicker({
        format: 'Y-m-d',
        maxDate: new Date(),
        yearEnd: 2017,
        timepicker: false,
      });
    }, 'ensure/datetimepicker');
  },
  bindReviseMobile: function() {
    // body...
    require.ensure([], function() {
      // body...

      let vm_reviseMobile = avalon.define({
        $id: 'mc_reviseMobile',
        mobile: '',
        code: '',
        time: '',
        getCodeText: '点击获取',
        disableClass: '',
        msg_error: false,
        msg_error_txt: '',
        bindSetError: function() {
          // 当输入内容改变时错误提示消失
          vm_reviseMobile.msg_error = false;
        },
        validate_reviseMobile: {
          onValidateAll: function(reasons) {
            if (reasons.length) {
              vm_reviseMobile.msg_error = true;
              vm_reviseMobile.msg_error_txt = reasons[0].getMessage();
            } else {
              cp.ajaxUrl({
                controller: 'account',
                cmd: 'com.rel.porg.pb.acc_mobile_bind_req',
                data: {
                  mobile: vm_reviseMobile.mobile,
                  vcode: vm_reviseMobile.code,
                },
                success: function(res) {
                  // body...
                  cp.layerMsg({msg: '绑定成功'});
                  vm_member.init();
                  cp.layerCloseAll();
                },
                fail: (res) => {
                  vm_reviseMobile.msg_error = true;
                  vm_reviseMobile.msg_error_txt = '验证码错误！';
                },
              });
            }
          },
        },
        bindGetCode: function(disable) {
          if (disable !== 'disable') {
            if (vm_reviseMobile.mobile === '') {
              vm_reviseMobile.msg_error = true;
              vm_reviseMobile.msg_error_txt = '请填写手机号！';
            } else {
              let reg = (/^1[3|4|5|7|8][0-9]{9}$/).test(vm_reviseMobile.mobile);
              if (reg) {
                cp.ajaxUrl({
                  controller: 'account',
                  cmd: 'com.rel.porg.pb.acc_send_verfiy_code_req',
                  data: {mobile: vm_reviseMobile.mobile, module: 3},
                  success: function(res) {
                    if (res.ret === 1) {
                      getTime(120);
                    }
                  },
                  fail: res => {
                    cp.layerMsg({
                      msg: res.desc,
                    });
                  },
                });
              } else {
                vm_reviseMobile.msg_error = true;
                vm_reviseMobile.msg_error_txt = '请填写正确的手机号！';
              }
            };
          }
        },
      });

      let page_reviseMobile = require('./_reviseMobile.html');
      cp.layer(layer => {
        layer.open({
          title: false,
          btn: 0,
          area: '498px',
          content: page_reviseMobile,
          success: function() {
            avalon.ready(function() {
              avalon.scan(document.querySelector('#j-reviseMobile'));
            });
          },
        });
      });
      function getTime(time) {
        // 倒计时60s
        if (time === 1) {
          vm_reviseMobile.getCodeText = '点击获取';
          vm_reviseMobile.disableClass = '';
          vm_reviseMobile.time = '';
          time = 120;
        } else {
          time--;
          vm_reviseMobile.time = time;
          setTimeout(function() {
            getTime(time);
          }, 1000);
          vm_reviseMobile.getCodeText = '重新获取';
          vm_reviseMobile.disableClass = 'disable';
        }
      }
    }, 'ensure/revisemobile');
  },
  init: () => {
    cp.ajaxUrl({
      controller: 'user',
      cmd: 'com.rel.porg.pb.user_info_req',
      data: {
        'req': 2,
      },
      success: function(res) {
        for (let i in vm_member.user_info) {
          if (res.info[i] && i !== 'account') {
            vm_member.user_info[i] = res.info[i];
          } else if (!res.info.account.realname) {
            res.info.account.realname = '';
            vm_member.user_info.account = res.info.account;
          } else {
            vm_member.user_info.account = res.info.account;
          }
        }
      },
      fail: function(res) {
      },
    });
  },
});
avalon.ready(function() {
  vm_member.init();
  vm_member.initQiniu();
  vm_member.initDatePicker();
});
